﻿
using Microsoft.Maui.Controls.PlatformConfiguration;
using Plugin.Maui.Audio;

namespace mdgame.Core.Scene_3
{
    public class Scene3 : Game
    {
        double win_w = 0, win_h = 0;
        private int score = 0;
        IAudioManager audioManager;
        GraphicsView ghv = null;
        Bird bird;
        Diren diren;
        public Scene3(GraphicsView ghv, IAudioManager audioManager)
        {
            this.audioManager = audioManager;
            this.ghv = ghv;
            //Task.Run(async () =>
            //{
            //    while (true)
            //    {
            //        if (ghv != null)
            //        {
            //            this.ghv.Invalidate();

            //        }
            //        await Task.Delay(1000);
            //    }
            //});

            bird = new Bird();
            diren = new Diren();

            bird.Create("mdgame.Resources.Images.ninjacat.png");
            bird.CreateMusic(audioManager, "sfx_ui_x04.wav");
            diren.Create("mdgame.Resources.Images.dino.png");


        }

        public override void Draw(ICanvas canvas, RectF dirtyRect)
        {
            bird.Draw(canvas);
            diren.Draw(canvas);
            // Draw the score

            canvas.FontColor = Brush.Black.Color;
            canvas.FontSize = 16;
            canvas.DrawString($"Score: {score}", 10, 10, dirtyRect.Width, dirtyRect.Height,
                HorizontalAlignment.Center,
                VerticalAlignment.Center);

            canvas.DrawString($"Bird: {bird.x},{bird.y}", 10, 30, dirtyRect.Width, dirtyRect.Height,
         HorizontalAlignment.Center,
         VerticalAlignment.Center);

            canvas.DrawString($"Diren: {diren.x},{diren.y}", 10, 50, dirtyRect.Width, dirtyRect.Height,
         HorizontalAlignment.Center,
         VerticalAlignment.Center);
            // canvas.DrawString(, 500, 10);
            //canvas.DrawText($"Bird: {bird.x},{bird.y}", 500, 30, Colors.Black);
            //canvas.DrawText($"Diren: {diren.x},{diren.y}", 500, 50, Colors.Black);

            diren.move();

            if (bird.Check(diren))
            {
                score += 100;
            }

            Application.Current.MainPage.Dispatcher.Dispatch(() =>
            {
                ghv.Invalidate();
            });
            //canvas.ResetState();

            //if (image != null)
            //{
            //    ImagePaint imagePaint = new ImagePaint
            //    {
            //        Image = image.Downsize(100)
            //    };
            //    canvas.SetFillPaint(imagePaint, RectF.Zero);
            //    canvas.FillRectangle(0, 0, 240, 300);
            //}

            //if (image != null)
            //{
            //    canvas.SetFillImage(image.Downsize(100));
            //    canvas.FillRectangle(0, 0, 240, 300);
            //}

            //bird.Draw(canvas);
            //bird.move();
            //if (image != null)
            //{
            //    canvas.DrawImage(image, 10, 10, image.Width, image.Height);
            //}
        }

        internal override void PanUpdated(object sender, PanUpdatedEventArgs e)
        {
        }
        internal override void Tapped(object sender, TappedEventArgs e)
        {
            bird.tapmove(sender, e);
        }
    }
}
